import sys
sys.path.append('F:\\python36\\Lib\\site-packages')

import socket               # 导入 socket 模块
import time
if True:
    #connType=int(input("(1) Get current time(TCP)\n(2)Echo Mode(UDP)\nChoose: "))
    connType=1
    srcAddr,srcPort = socket.gethostbyname(socket.gethostname()) , 9559
    dstAddr,dstPort = "180.101.49.12" , 80

    try:
        if(connType==1):
            with socket.socket(socket.AF_INET,socket.SOCK_STREAM,socket.IPPROTO_IP) as tcpSocket: 
                tcpSocket.connect((dstAddr,dstPort))
                requestLine="GET / HTTP/1.0\r\nHost: www.baidu.com\r\n\r\n"
                tcpSocket.send(bytes(requestLine,encoding="utf-8"))
                while True:
                    message=tcpSocket.recv(65535)
                    if(not message):
                        break
                    print(str(message,encoding="utf-8"))
                    time.sleep(1)
        else:
            with socket.socket(socket.AF_INET,socket.SOCK_DGRAM) as udpSocket:   
                udpSocket.sendto(bytes("HELLO SERVER!I am client one!",encoding="utf-8"), (dstAddr,dstPort))
                message=udpSocket.recv(1024)
                print(message)
    except Exception as e:
        print(e)